import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Condition } from '../classes';
import { StoreService, queryCondition } from '../store.service';
import { DataService } from '../data.service';

@Component({
  selector: 'app-mrcondition',
  templateUrl: './mrcondition.component.html',
  styleUrls: ['./mrcondition.component.less'],
})
export class MrconditionComponent implements OnInit {
  constructor(public store: StoreService, public dataService: DataService) {}

  ngOnInit() {}
  @Input() currentlist: Condition[] = [];
  @Input() showSelect: boolean = true;

  @Output('oncurrentlistChanged') currentlistChanged: EventEmitter<
    Condition[]
  > = new EventEmitter<Condition[]>();
  public oncurrentlistChanged() {
    this.currentlistChanged.emit(this.currentlist);
  }

  currentitem: Condition;

  showselect: boolean = false;
  valueDic = {};
  dic: any[] = [];
  currentShowColumn = [];
  selectitemValue = [];
  showConditionList() {
    this.showlist = !this.showlist;
    this.selectConditions = [];
    this.dataService.clearSelection();
    // this.store.conditionlist.forEach(v => {
    //   v.selected = false
    // })
  }
  // getInfo(condition: Condition): queryCondition {
  //   console.log(condition);
  //   return this.dataService.getConditionInfo(condition);
  // }
  // isCommonInput(condition: Condition) {
  //   console.log("iscommon");
  //   let info = this.dataService.getConditionInfo(condition);
  //   let r = info.datatype == "" && info.dictable == "";
  //   console.log(r);
  //   return r;
  // }
  // isSelectInput(condition: Condition) {
  //   if (condition === undefined) return false;
  //   console.log("isselect");
  //   let info = this.dataService.getConditionInfo(condition);
  //   let r = info.dictable != "";
  //   console.log(r);
  //   if (r) {
  //     if (
  //       this.valueDic === undefined ||
  //       Object.keys(this.valueDic).findIndex(s => s === info.show) < 0
  //     ) {
  //       this.valueDic[info.show] = this.getDic(condition);
  //     }
  //   }
  //   return r;
  // }
  selectConditions: string[] = [];
  changecondition($event) {
    let v = $event.target.value;
    console.log(v);
    if (this.selectConditions.includes(v))
      this.selectConditions.splice(this.selectConditions.indexOf(v), 1);
    else this.selectConditions.push(v);
  }
  target = '';
  async openadditionselect(item: Condition) {
    this.target = 'additional';
    await this.openselect(item);
  }
  async openselect(item: Condition) {
    this.currentitem = item;
    this.currentShowColumn = item.DicColumns.split(',');
    this.showselect = true;
    console.log('load dic');
    this.dic = [];
    if (item.Options.length > 0) {
      item.Options.forEach((option) => {
        this.dic.push(option);
      });
      //this.dic = item.Options;
    } else {
      let r = await this.dataService.getDic(item);
      r.forEach((v) => this.dic.push(v));
    }
    console.log(this.dic);
  }
  async getDic(item: Condition) {
    if (item === undefined) return [];
    console.log('get dic');
    let r = [];
    r = await this.dataService.getDic(item);
    return r;
  }
  selectValues() {
    console.log(this.currentitem);
    console.log(this.selectitemValue);
    console.log(this.currentitem);

    if (this.selectitemValue.length > 0) {
      if (this.target !== 'additional' || this.target === undefined) {
        // this.currentitem.Value = this.selectitemValue[0];
        // this.currentitem.Value = "";
        this.currentitem.Value = this.selectitemValue
          .map((v) => v[Object.keys(v)[0]])
          .join(',');
      }
      if (this.target === 'additional') {
        // this.currentitem.AddtionalValue = this.selectitemValue[0];
        // this.currentitem.AddtionalValue = "";
        this.currentitem.AddtionalValue = this.selectitemValue
          .map((v) => v[Object.keys(v)[0]])
          .join(',');
      }
    }
    this.target = '';
    this.showselect = false;
  }
  selctItems() {
    if (this.selectConditions !== undefined) {
      let l = this.dataService.selectitem(this.selectConditions);
      l.forEach((item) => {
        this.currentlist.push(item);
        //if (this.autoAddToShwoColumns) this.currentShowColumn.push(item.Target);
      });
      // this.selectConditions.forEach(v => {
      //   console.log(v);
      //   let c = new Condition();
      //   c.Target = v;
      //   // let s = this.store.conditionlist.find(x => x.name == v);
      //   // console.log(s);
      //   // c.Show = s.show;

      //   this.currentlist.push(c);
      // });
    }
    this.selectConditions = [];
    this.showlist = false;
  }
  cancelItems() {
    this.selectConditions = [];
    this.showlist = false;
  }
  // public get currentlist(): Condition[] {
  //   return this._currentlist;
  // }
  // public set currentlist(value: Condition[]) {
  //   this._currentlist = value;
  // }
  showlist: boolean = false;
  // addCondition() {
  //   this.showlist = !this.showlist;
  // }
  select(item) {
    console.log(item);
    let c = new Condition();
    c.Target = item;
    this.currentlist.push(c);
    this.showlist = false;
  }
  hide() {
    this.showlist = false;
  }
  remove(item) {
    var i = this.currentlist.findIndex((v) => v === item);
    if (i > -1) this.currentlist.splice(i, 1);
  }
  moveup(item) {
    var i = this.currentlist.findIndex((v) => v === item);
    if (i > -1) {
      this.currentlist.splice(i, 1);
      this.currentlist.splice(i - 1, 0, item);
    }
  }
  movedown(item) {
    var i = this.currentlist.findIndex((v) => v === item);
    if (i > -1) {
      this.currentlist.splice(i, 1);
      this.currentlist.splice(i + 1, 0, item);
    }
  }
}
